
import storage from './utils/storage.js'
import {host} from './utils/host'
// 防抖方法
import metheds from './utils/common.js'
// 全局配置
let common = {
    barUrl: host.serverHost,
    header: {
        'Content-Type': 'application/json;charset=UTF-8'
    },
    data: {},
    method: 'GET',
    dataType: 'json'
}

let codes = {
	'其他': '未知错误',
	'20010': '原密码不正确',
	'20012': '没有权限',
	'20015': '验证码不正确',
	'20003': '用户已存在',
	'20000': '服务器繁忙',
	'20001': '参数错误',
	'20013': '索引已经存在',
	'20006': 'token已过期',
	'20004': '手机号已被注册',
	'20008': '用户名或密码不正确',
	'20005': '无效的token',
	'20011': '账户被禁用',
	'20014': '时间超出范围',
	'20002': '未登录',
	'20007': 'Authorization 格式不正确',
	'20009': '数据不存在',
}
export default {
    request(options = {}) {
        options.url = common.barUrl + options.url
        options.header = options.header || common.header
        options.data = options.data || common.data
        options.method = options.method || common.method
        options.dataType = options.dataType || common.dataType

        // 验证是否需要带token 布尔
        if (options.token) {
			options.header.token = storage.getToken()
        } else {
            options.header = {
                'Content-Type': 'application/json;charset=UTF-8'
            }
        }

        // 请求
		let that = this;
        return new Promise((res, resError) => {
            uni.request({
                ...options,
                success: result => {
                    // console.log(result);
					// return;
                    if (result.statusCode !== 200) {
                        return resError(result.data);
                    }
					if(result.data.err&&result.data.err.indexOf('token')!=-1&&!options.noOut){
						metheds.debounce(()=>{
							uni.showToast({title: '登录已失效!',icon:'none'})
							setTimeout(()=>{
								storage.clearToken('token');
								storage.clearUser('user');
								uni.reLaunch({url: '/pages/login'});
							},1000)
						});
						return resError(result.data);
					}
					// 成功
					res(result.data);
                },
                fail: (error) => {
					// console.log(error);
                    return resError('请求失败');
                },
            })
        })
    },
    // get请求
    get(url, data = {}, options = {}) {
        options.url = url
        options.data = data
        options.method = 'GET'
        return this.request(options)
    },
    // post请求
    post(url, data = {}, options = {}) {
        options.url = url
        options.data = data
        options.method = 'POST'
        return this.request(options)
    },
    //put请求
    put(url, data = {}, options = {}) {
        options.url = url
        options.data = data
        options.method = 'PUT'
        return this.request(options)
    },
    delete1(url, data = {}, options = {}) {
        options.url = url
        options.data = data
        options.method = 'DELETE'
        return this.request(options)
    },
    delete(url, data = {}, options = {}) {
        options.url = `${url}?id=${data.id}`
        options.data = {}
        options.method = 'DELETE'
        return this.request(options)
    }
};